package com.example.touch.domain;

import java.util.Calendar;

import android.graphics.Point;

import com.example.touch.communication.entities.TimeTable;
import com.example.touch.location.IEvaluableObjectLocation;
import com.example.touch.time.EvaluableTimeTable;
import com.example.touch.time.IEvaluableTimeTable;

public class EventTrigger 
{
	private int radius;
	private int tolerance;
	private IEvaluableObjectLocation referencedLocation;
	private TimeTable timeTable;
	
	public EventTrigger(int radius, int tolerance, IEvaluableObjectLocation refLocation, TimeTable timeTable)
	{
		this.radius = radius;
		this.tolerance = tolerance;
		this.referencedLocation = refLocation;
		this.timeTable = timeTable;
	}
	
	public boolean evaluate(Point currentPosition, Calendar now)
	{
		boolean isWithinLocation = false, isWithinTime = false;
		
		if(referencedLocation != null)
			isWithinLocation = referencedLocation.isWithin(currentPosition, radius);
		
		isWithinTime = timeTable != null && now != null ? evaluateTimeTable(now) : true;
		
		return isWithinLocation && isWithinTime;
	}
	
	private boolean evaluateTimeTable(Calendar now)
	{
		IEvaluableTimeTable evaluableTimeTable = new EvaluableTimeTable(timeTable);
		return evaluableTimeTable.isWithin(now);
	}
	
	public int getRadius()
	{
		return radius;
	}
	
	public int getTolerance()
	{
		return tolerance;
	}
	
	public IEvaluableObjectLocation getLocation()
	{
		return referencedLocation;
	}
	
	public TimeTable getTimeTable()
	{
		return timeTable;
	}
	
	public void setRadius(int radius)
	{
		this.radius = radius;
	}
	
	public void setTolerance(int tolerance)
	{
		this.tolerance = tolerance;
	}
	
	public void setLocation(IEvaluableObjectLocation newLocation)
	{
		this.referencedLocation = newLocation;
	}
	
	public void setTimeTable(TimeTable newTimeTable)
	{
		this.timeTable = newTimeTable;
	}
}
